﻿using System;
using System.Linq;
using JetBrains.ReSharper.Daemon;
using JetBrains.ReSharper.Daemon.Stages;
using JetBrains.ReSharper.Daemon.Xml.Highlightings;
using JetBrains.ReSharper.Psi.Tree;
using JetBrains.ReSharper.Psi.Xml;
using JetBrains.ReSharper.Psi.Xml.Tree;
using SPCAFContrib.ReSharper.Common;
using SPCAFContrib.ReSharper.Common.Attributes;
using SPCAFContrib.ReSharper.Common.Extensions;
using SPCAFContrib.ReSharper.Common.XmlAnalysis;
using SPCAFContrib.ReSharper.Consts;
using SPCAFContrib.ReSharper.Inspection.Xml.SPCAF;

[assembly: RegisterConfigurableSeverity(SPC045401Highlighting.CheckId,
  null,
  Consts.SPCAF_DESIGN_GROUP,
  SPC045401Highlighting.CheckId + ": " + SPC045401Highlighting.Message,
  "If the ListInstance seems to be used for an external list, the correct TemplateType (600) should be used.",
  Severity.WARNING,
  false, Internal = false)]

namespace SPCAFContrib.ReSharper.Inspection.Xml.SPCAF
{
    [Applicability(
        IDEProjectType.SP2010FarmSolution |
        IDEProjectType.SPSandbox |
        IDEProjectType.SP2013FarmSolution)]
    public class DeclareTemplateTypeForExternalList : XmlTagProblemAnalyzer
    {
        public override void Run(IXmlTag element, IHighlightingConsumer consumer)
        {
            if (element.GetProject().IsApplicableFor(this))
            {
                if (IsInvalid(element))
                {
                    SPC045401Highlighting errorHighlighting =
                        new SPC045401Highlighting(element);
                    consumer.ConsumeHighlighting(element.Header.GetDocumentRange(), errorHighlighting);
                }
            }
        }

        public static bool IsInvalid(IXmlTag element)
        {
            bool result = false;
            var children = element.GetNestedTags<IXmlTag>("DataSource/Property");
            if (element.Header.ContainerName == "ListInstance" &&
                children.Any(tag => tag.CheckAttributeValue("Name",new[] {"LobSystemInstance"})) &&
                children.Any(tag => tag.CheckAttributeValue("Name",new[] {"EntityNamespace"})))
            {
                result = !element.AttributeExists("FeatureId") ||
                         (!element.AttributeExists("TemplateType") ||
                          !element.CheckAttributeValue("TemplateType", new[] {"600"}));
            }

            return result;
        }
    }

    [ConfigurableSeverityHighlighting(CheckId, XmlLanguage.Name, OverlapResolve = OverlapResolveKind.NONE, ShowToolTipInStatusBar = true)]
    public class SPC045401Highlighting : XmlErrorHighlighting
    {
        public const string CheckId = "SPC045401";
        public const string Message = "Declare 'TemplateType' and 'FeatureId' for external ListInstance";

        public IXmlTag Element { get; private set; }

        public SPC045401Highlighting(IXmlTag element) :
            base(String.Format("{0}: {1}", CheckId, Message))
        {
            Element = element;
        }

        public override bool IsValid()
        {
            return this.Element.IsValid();
        }
    }
}
